package ${package}.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import ${package}.mapper.${entity}Mapper;
import ${package}.po.${entity};
import ${package}.po.${entity}Example;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
/**
* ${entity} 业务处理类
* @author ${author}
* @date   ${date}
*/
@Service
@Transactional
public class ${entity}Service {
    /**${entity}数据访问层对象*/
    @Resource
    private ${entity}Mapper ${entity?uncap_first}Mapper ;

    /**
    * 查询所有 ${entity} 状态为未删除 is_deleted=0 的数据
    * @return ${entity}所有的数据
    */
    public List<${entity}> list(){
        ${entity}Example ${entity?uncap_first}Example = new ${entity}Example();
        //查询 数据表中 is_deleted = 0
        ${entity?uncap_first}Example.createCriteria().andIsDeletedEqualTo(false) ;
        return ${entity?uncap_first}Mapper.selectByExample(${entity?uncap_first}Example) ;
    }

    /**
    * 根据删除主键删除 ${entity} 逻辑删除 进行is_deleted=1的修改操作
    * @param id ${entity}的主键
    * @return true 删除成功  false 删除失败
    */
    public boolean remove(Long id){
        ${entity} ${entity?uncap_first} = new ${entity}();
        ${entity?uncap_first}.setId(id);
        ${entity?uncap_first}.setIsDeleted(true);
        return ${entity?uncap_first}Mapper.updateByPrimaryKeySelective(${entity?uncap_first}) > 0 ;
    }

    /**
    * 新增一条${entity}记录
    * @param  ${entity?uncap_first} 封装新增的数据
    * @return true 新增成功  false 新增失败
    */
    public boolean save(${entity} ${entity?uncap_first}){
        ${entity?uncap_first}.setCreateTime(new Date());
        ${entity?uncap_first}.setUpdateTime(new Date());
        ${entity?uncap_first}.setIsDeleted(false);
        return ${entity?uncap_first}Mapper.insert(${entity?uncap_first})>0 ;
    }

    /**
    * 根据主键查询 ${entity}
    * @param id ${entity}的主键
    * @return null id不存在  返回${entity}对象
    */
    public ${entity} get(Long id){
        return ${entity?uncap_first}Mapper.selectByPrimaryKey(id) ;
    }

    /**
    * 修改一条${entity}记录
    * @param  ${entity?uncap_first} 封装修改的数据
    * @return true 修改成功  false 修改失败
    */
    public boolean update(${entity} ${entity?uncap_first}) {
        ${entity?uncap_first}.setUpdateTime(new Date());
        return ${entity?uncap_first}Mapper.updateByPrimaryKeySelective(${entity?uncap_first})>0 ;
    }

    /**
    * 分页查询 ${entity}  如果有查询条件需要动态在方法体内添加条件
    * @param pageNum 页码
    * @param pageSize 分页跨度
    * @param ${entity?uncap_first} 封装查询的数据
    * @return 分页对象
    */
    public PageInfo<${entity}> listPage(Long pageNum, Long pageSize,${entity} ${entity?uncap_first}) {
        PageHelper.startPage(pageNum.intValue(),pageSize.intValue());
        ${entity}Example ${entity?uncap_first}Example = new ${entity}Example();
        ${entity}Example.Criteria criteria = ${entity?uncap_first}Example.createCriteria();
        return new PageInfo<>(${entity?uncap_first}Mapper.selectByExample(${entity?uncap_first}Example));
    }
}